package com.zrt.bookstore3.Controller;

import com.zrt.bookstore3.Domain.User;
import com.zrt.bookstore3.Service.UserService;
import com.zrt.bookstore3.utils.CodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/login")
public class Login {

    @Autowired
    private UserService userService;

    @Autowired
    private CodeUtils codeUtils;
    //实现注册功能
    @RequestMapping(value = "/register",method = {RequestMethod.POST, RequestMethod.GET})
    public String signUp(HttpServletRequest request){
        String permission = null;
        String code1 = request.getParameter("code");

        System.out.println("-----------------------------");
        System.out.println(code1);

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email =  request.getParameter("email");

        System.out.println(email);

        String telephone = request.getParameter("telephone");

        System.out.println(telephone);

        String code = codeUtils.get(telephone);

        System.out.println(code);
        System.out.println("+++++++++++++++++++++++++++++");
        if (code1.equals(code)){
            User user = new User(0,username,password,email,telephone);
            Boolean insert = userService.insert(user);
            permission = insert ? "redirect:/pages/Login.html" : "redirect:/pages/registered.html";
        }

        return permission;
    }


    @RequestMapping(value="/verification",method = {RequestMethod.POST, RequestMethod.GET})        // 失败错误信息页面网址
    public String verification( HttpServletRequest request) {

        String username = request.getParameter("username");
        String password = request.getParameter("password");

        String permission = null;

        if (" ".equals(username) || " ".equals(password)) {   // 密码为空，重新登录
//            return "/pages/tip.html";
            permission = "redirect:/pages/tip.html";
        } else {
            User user1 = userService.selectUserByUsernameAndPassword(username, password);  // 从数据库中获得与输入相同的用户
            if (user1 == null) {         // 没有该用户
                return "redirect:/pages/noTheUser.html";
            }
            // 创建 session
            HttpSession session = request.getSession();
            session.setAttribute("user", user1);

            String role = user1.getRole();

            if (role.equals("用户")) {
                //重定向到学生的初始界面
                permission = "redirect:/pages/user.html";
            }  else if (role.equals("管理员")) {
                //重定向到宿管的初始界面
                permission = "redirect:/pages/manager.html";
            }

        }
        return permission;
    }



}
